package learnjava.basic.other;

import java.util.Arrays;
import java.util.Comparator;

public class TestCompare implements Comparable<TestCompare> {

    public static void main(String[] args) {
        TestCompare[] a = new TestCompare[]{new TestCompare(3),new TestCompare(2),new TestCompare(4)};
        Arrays.sort(a, new Comparator<TestCompare>() {
            @Override
            public int compare(TestCompare o1, TestCompare o2) {
                return o2.age-o1.age;
            }
        });
        System.out.println(Arrays.asList(a));
    }

    int age;

    public TestCompare(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(TestCompare o) {
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "TestCompare{" +
                "age=" + age +
                '}';
    }
}
